//浏览器升级提示
if(!!window.ActiveXObject && document.documentMode < 8) {
    location.href = '/Public/ieUpdate.html';
}

//防止console对象不存在页面出错
var console = window.console || {log: function(){}};

//requireJs配置
require.config({
	urlArgs: 'bust=' + 3,
	baseUrl: '/js/',
	shim: {
		jqueryui: ['jquery'],
		cookie: ['jquery'],
		dialog: ['jqueryui'],
		validate: ['jquery'],
		validateMessage: ['jquery', 'validate'],
		icheck: ['jqueryui'],
		mousewheel: ['jquery'],
		imageGroup: ['dialog', 'fancyBox', 'ajaxupload'],
		fancyBox: ['jquery', 'mousewheel'],
		ajaxupload:['jquery'],
		form: ['jquery', 'validate', 'validateMessage'],
		md5: ['jquery'],
		pager: ['jquery'],
		suiAjaxSubmit: ['form', 'dialog'],
		suiAjax: ['jquery', 'dialog'],
		dateRange: ['jquery', 'jqueryui', 'dialog'],
		slimscroll: ['jquery'],
		suggest: ['jquery'],
		wechatMenu: ['jquery'],
		calendar: ['jquery']
	},
	paths: {
		jquery: '/base/javascripts/jquery-1.12.3.min',
		jqueryui: '/jquery-ui/jquery-ui.min',
		validateMessage: 'lib/localization/messages_zh.min',
		cookie: '/base/javascripts/jquery.cookie.min',
		dialog: '/dialog/jquery.dialog.min',
		common: '/base/javascripts/common',
		WdatePicker: '/my97/WdatePicker',
		validate: '/base/javascripts/jquery.validate.min',
		pager: '/base/javascripts/jquery.pager.min',
		icheck: '/base/javascripts/jquery.icheck.min',
		suiAjax: '/base/javascripts/sui.ajax.min',
		form: '/base/javascripts/jquery.form.min',
		validateMessage: '/base/javascripts/messages_zh.min',
		suiAjaxSubmit: '/base/javascripts/sui.ajaxSubmit.min',
		
	}
});

//静态资源路径
var RES_URL = 'res/';

//自动载入模块
var APP_CONFIG = [ 'jquery','jqueryui', 'icheck', 'cookie', 'dialog', 'suiAjaxSubmit', 'suiAjax'];

/**
 * 自动载入
 */
require(['jquery', 'form'], function($) {

	// 禁止backspace键
	$(document).keydown(function(event) {
		if (event.keyCode == 8 && !$("input,textarea").is(":focus")) {
			event.preventDefault();
		}
	});

	// 统一添加搜索框回车搜索的事件
	$('.search-bar input').on('keyup', function(event){
		if(event.keyCode == 13){
			var searchBtn = $('#searchBtn');
			if(searchBtn[0]){
				searchBtn.trigger('click');
			}
		}
	});

});

/**
 * 分页控件方法
 * 调用方法示例
 * var getList = function(queryData){
 * 	   $.get('xxx', queryData, function(result){
 * 	   	   if(Ok){
 * 	   	   	   var data = result.data;
 *			   queryData.pageNumber = data.pageNumber;
 *			   queryData.pageCount = data.pageCount;
 *			   pager(queryData, getList);
 * 	   	   }
 * 	   })
 * }
 */
/*function pager(data, callback, pagerId) {
	pagerId = pagerId || '#pager';
	$(pagerId).pager({
		pagenumber: data.pageNumber || 1,  //当前页面
		pagecount: data.pageCount || 1,   //总页数
		buttonClickCallback: function(p) {
			data.pageNumber = p;
			if(callback){
				callback(data);
			}
		}
	});
}*/
var Pager = function() {
	defaultParams = {
		selector: '#pager', // 分页控件容器的选择器
		pagenumber: 1,  //当前页面
		pagecount: 1,   //总页数
		callback: null // 点击页码时的回调(拉取列表并渲染的方法)
	};
	var params = {};
	var currentPage = 1; //当前页面
	var pageCount = 1;  //总页数
	var selector = '#pager'; // 分页控件容器的选择器
	var callback = null; // 点击页码时的回调(拉取列表并渲染的方法)
	function update(data){
		var _self = this;
		$(_self.selector).pager({
			pagenumber: _self.page,  //当前页面
			pagecount: _self.pageCount,   //总页数
			buttonClickCallback: function(p) {
				this.page = p;
				if(callback){
					callback(data);
				}
			}
		});
	}
	function init(selector, callback ){
		this.selector = selector || this.selector;
		this.callback = callback || this.callback
	}
	function pager(data, callback, pagerId){
		pagerId = pagerId || '#pager';
		var _self = this;
		$(pagerId).pager({
			pagenumber: this.page,  //当前页面
			pagecount: this.pageCount,   //总页数
			buttonClickCallback: function(p) {
				_self.page = p;
				if(callback){
					callback(data);
				}
			}
		});
	}
};

var pager = function (data, callback, pagerId){
	pagerId = pagerId || '#pager';
	$(pagerId).pager({
		pagenumber: data.page || 1,  //当前页面
		pagecount: data.pageCount || 1,   //总页数
		buttonClickCallback: function(p) {
			location.hash = p;
            data.page = p;
            if(callback){
				callback(data);
                $('html,body').animate({scrollTop:0}, 'fast');
			}
		}
	});
};
